{
    "cells": [
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "<i>Copyright (c) Recommenders contributors.</i>\n",
                "\n",
                "<i>Licensed under the MIT License.</i>"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "# Evaluation"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "Evaluation with offline metrics is pivotal to assess the quality of a recommender before it goes into production. Usually, evaluation metrics are carefully chosen based on the actual application scenario of a recommendation system. It is hence important to data scientists and AI developers that build recommendation systems to understand how each evaluation metric is calculated and what it is for.\n",
                "\n",
                "This notebook deep dives into several commonly used evaluation metrics, and illustrates how these metrics are used in practice. The metrics covered in this notebook are merely for off-line evaluations."
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## 0 Global settings"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "Most of the functions used in the notebook can be found in the `recommenders` directory."
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 36,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "System version: 3.9.16 (main, May 15 2023, 23:46:34) \n",
                        "[GCC 11.2.0]\n",
                        "Pandas version: 1.5.3\n",
                        "PySpark version: 3.2.4\n",
                        "Scikit Learn version: 1.0.2\n"
                    ]
                }
            ],
            "source": [
                "import sys\n",
                "import pandas as pd\n",
                "import pyspark\n",
                "import sklearn\n",
                "from sklearn.preprocessing import minmax_scale\n",
                "\n",
                "from recommenders.utils.spark_utils import start_or_get_spark\n",
                "from recommenders.evaluation.spark_evaluation import SparkRankingEvaluation, SparkRatingEvaluation\n",
                "from recommenders.evaluation.python_evaluation import auc, logloss\n",
                "\n",
                "print(f\"System version: {sys.version}\")\n",
                "print(f\"Pandas version: {pd.__version__}\")\n",
                "print(f\"PySpark version: {pyspark.__version__}\")\n",
                "print(f\"Scikit Learn version: {sklearn.__version__}\")"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "Note to successfully run Spark codes with the Jupyter kernel, one needs to correctly set the environment variables of `PYSPARK_PYTHON` and `PYSPARK_DRIVER_PYTHON` that point to Python executables with the desired version. Detailed information can be found in the setup instruction document [SETUP.md](../../SETUP.md)."
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 2,
            "metadata": {},
            "outputs": [],
            "source": [
                "COL_USER = \"UserId\"\n",
                "COL_ITEM = \"MovieId\"\n",
                "COL_RATING = \"Rating\"\n",
                "COL_PREDICTION = \"Rating\"\n",
                "\n",
                "HEADER = {\n",
                "    \"col_user\": COL_USER,\n",
                "    \"col_item\": COL_ITEM,\n",
                "    \"col_rating\": COL_RATING,\n",
                "    \"col_prediction\": COL_PREDICTION,\n",
                "}"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## 1 Prepare data"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "### 1.1 Prepare dummy data"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "For illustration purpose, a dummy data set is created for demonstrating how different evaluation metrics work. \n",
                "\n",
                "The data has the schema that can be frequently found in a recommendation problem, that is, each row in the dataset is a (user, item, rating) tuple, where \"rating\" can be an ordinal rating score (e.g., discrete integers of 1, 2, 3, etc.) or an numerical float number that quantitatively indicates the preference of the user towards that item. \n",
                "\n",
                "For simplicity reason, the column of rating in the dummy dataset we use in the example represent some ordinal ratings."
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 3,
            "metadata": {},
            "outputs": [],
            "source": [
                "df_true = pd.DataFrame(\n",
                "        {\n",
                "            COL_USER: [1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],\n",
                "            COL_ITEM: [1, 2, 3, 1, 4, 5, 6, 7, 2, 5, 6, 8, 9, 10, 11, 12, 13, 14],\n",
                "            COL_RATING: [5, 4, 3, 5, 5, 3, 3, 1, 5, 5, 5, 4, 4, 3, 3, 3, 2, 1],\n",
                "        }\n",
                "    )\n",
                "df_pred = pd.DataFrame(\n",
                "    {\n",
                "        COL_USER: [1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],\n",
                "        COL_ITEM: [3, 10, 12, 10, 3, 5, 11, 13, 4, 10, 7, 13, 1, 3, 5, 2, 11, 14],\n",
                "        COL_PREDICTION: [14, 13, 12, 14, 13, 12, 11, 10, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5]\n",
                "    }\n",
                ")"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "Take a look at ratings of the user with ID \"1\" in the dummy dataset."
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 4,
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "text/html": [
                            "<div>\n",
                            "<style scoped>\n",
                            "    .dataframe tbody tr th:only-of-type {\n",
                            "        vertical-align: middle;\n",
                            "    }\n",
                            "\n",
                            "    .dataframe tbody tr th {\n",
                            "        vertical-align: top;\n",
                            "    }\n",
                            "\n",
                            "    .dataframe thead th {\n",
                            "        text-align: right;\n",
                            "    }\n",
                            "</style>\n",
                            "<table border=\"1\" class=\"dataframe\">\n",
                            "  <thead>\n",
                            "    <tr style=\"text-align: right;\">\n",
                            "      <th></th>\n",
                            "      <th>UserId</th>\n",
                            "      <th>MovieId</th>\n",
                            "      <th>Rating</th>\n",
                            "    </tr>\n",
                            "  </thead>\n",
                            "  <tbody>\n",
                            "    <tr>\n",
                            "      <th>0</th>\n",
                            "      <td>1</td>\n",
                            "      <td>1</td>\n",
                            "      <td>5</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>1</th>\n",
                            "      <td>1</td>\n",
                            "      <td>2</td>\n",
                            "      <td>4</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>2</th>\n",
                            "      <td>1</td>\n",
                            "      <td>3</td>\n",
                            "      <td>3</td>\n",
                            "    </tr>\n",
                            "  </tbody>\n",
                            "</table>\n",
                            "</div>"
                        ],
                        "text/plain": [
                            "   UserId  MovieId  Rating\n",
                            "0       1        1       5\n",
                            "1       1        2       4\n",
                            "2       1        3       3"
                        ]
                    },
                    "execution_count": 4,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "df_true[df_true[COL_USER] == 1]"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 5,
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "text/html": [
                            "<div>\n",
                            "<style scoped>\n",
                            "    .dataframe tbody tr th:only-of-type {\n",
                            "        vertical-align: middle;\n",
                            "    }\n",
                            "\n",
                            "    .dataframe tbody tr th {\n",
                            "        vertical-align: top;\n",
                            "    }\n",
                            "\n",
                            "    .dataframe thead th {\n",
                            "        text-align: right;\n",
                            "    }\n",
                            "</style>\n",
                            "<table border=\"1\" class=\"dataframe\">\n",
                            "  <thead>\n",
                            "    <tr style=\"text-align: right;\">\n",
                            "      <th></th>\n",
                            "      <th>UserId</th>\n",
                            "      <th>MovieId</th>\n",
                            "      <th>Rating</th>\n",
                            "    </tr>\n",
                            "  </thead>\n",
                            "  <tbody>\n",
                            "    <tr>\n",
                            "      <th>0</th>\n",
                            "      <td>1</td>\n",
                            "      <td>3</td>\n",
                            "      <td>14</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>1</th>\n",
                            "      <td>1</td>\n",
                            "      <td>10</td>\n",
                            "      <td>13</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>2</th>\n",
                            "      <td>1</td>\n",
                            "      <td>12</td>\n",
                            "      <td>12</td>\n",
                            "    </tr>\n",
                            "  </tbody>\n",
                            "</table>\n",
                            "</div>"
                        ],
                        "text/plain": [
                            "   UserId  MovieId  Rating\n",
                            "0       1        3      14\n",
                            "1       1       10      13\n",
                            "2       1       12      12"
                        ]
                    },
                    "execution_count": 5,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "df_pred[df_pred[COL_USER] == 1]"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "### 1.2 Prepare Spark data"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "Spark framework is sometimes used to evaluate metrics given datasets that are hard to fit into memory. In our example, Spark DataFrames can be created from the Python dummy dataset."
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "spark = start_or_get_spark(\"EvaluationTesting\", \"local\")\n",
                "\n",
                "dfs_true = spark.createDataFrame(df_true)\n",
                "dfs_pred = spark.createDataFrame(df_pred)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 7,
            "metadata": {},
            "outputs": [
                {
                    "name": "stderr",
                    "output_type": "stream",
                    "text": [
                        "\r",
                        "[Stage 0:>                                                          (0 + 1) / 1]\r"
                    ]
                },
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "+------+-------+------+\n",
                        "|UserId|MovieId|Rating|\n",
                        "+------+-------+------+\n",
                        "|     1|      1|     5|\n",
                        "|     1|      2|     4|\n",
                        "|     1|      3|     3|\n",
                        "+------+-------+------+\n",
                        "\n"
                    ]
                },
                {
                    "name": "stderr",
                    "output_type": "stream",
                    "text": [
                        "\r",
                        "                                                                                \r"
                    ]
                }
            ],
            "source": [
                "dfs_true.filter(dfs_true[COL_USER] == 1).show()"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 8,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "+------+-------+------+\n",
                        "|UserId|MovieId|Rating|\n",
                        "+------+-------+------+\n",
                        "|     1|      3|    14|\n",
                        "|     1|     10|    13|\n",
                        "|     1|     12|    12|\n",
                        "+------+-------+------+\n",
                        "\n"
                    ]
                }
            ],
            "source": [
                "dfs_pred.filter(dfs_pred[COL_USER] == 1).show()"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## 2 Evaluation metrics"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "### 2.1 Rating metrics\n",
                "\n",
                "Rating metrics are similar to regression metrics used for evaluating a regression model that predicts numerical values given input observations. In the context of recommendation system, rating metrics are to evaluate how accurate a recommender is to predict ratings that users may give to items. Therefore, the metrics are **calculated exactly on the same group of (user, item) pairs that exist in both ground-truth dataset and prediction dataset** and **averaged by the total number of users**.\n",
                "\n",
                "#### 2.1.1 Use cases\n",
                "\n",
                "Rating metrics are effective in measuring the model accuracy. However, in some cases, the rating metrics are limited if\n",
                "* **the recommender is to predict ranking instead of explicit rating**. For example, if the consumer of the recommender cares about the ranked recommended items, rating metrics do not apply directly. Usually a relevancy function such as top-k will be applied to generate the ranked list from predicted ratings in order to evaluate the recommender with other metrics. \n",
                "* **the recommender is to generate recommendation scores that have different scales with the original ratings (e.g., the SAR algorithm)**. In this case, the difference between the generated scores and the original scores (or, ratings) is not valid for measuring accuracy of the model.\n",
                "\n",
                "#### 2.1.2 How to work with the evaluation utilities\n",
                "\n",
                "A few notes about the interface of the Rating evaluator class:\n",
                "1. The columns of user, item, and rating (prediction) should be present in the ground-truth DataFrame (prediction DataFrame).\n",
                "2. There should be no duplicates of (user, item) pairs in the ground-truth and the prediction DataFrames, othewise there may be unexpected behavior in calculating certain metrics.\n",
                "3. Default column names for user, item, rating, and prediction are \"UserId\", \"ItemId\", \"Rating\", and \"Prediciton\", respectively.\n",
                "\n",
                "In our examples below, to calculate rating metrics for input data frames in Spark, a Spark object, `SparkRatingEvaluation` is initialized. The input data schemas for the ground-truth dataset and the prediction dataset are\n",
                "\n",
                "* Ground-truth dataset.\n",
                "\n",
                "|Column|Data type|Description|\n",
                "|-------------|------------|-------------|\n",
                "|`COL_USER`|<int\\>|User ID|\n",
                "|`COL_ITEM`|<int\\>|Item ID|\n",
                "|`COL_RATING`|<float\\>|Rating or numerical value of user preference.|\n",
                "\n",
                "* Prediction dataset.\n",
                "\n",
                "|Column|Data type|Description|\n",
                "|-------------|------------|-------------|\n",
                "|`COL_USER`|<int\\>|User ID|\n",
                "|`COL_ITEM`|<int\\>|Item ID|\n",
                "|`COL_RATING`|<float\\>|Predicted rating or numerical value of user preference.|"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 9,
            "metadata": {},
            "outputs": [
                {
                    "name": "stderr",
                    "output_type": "stream",
                    "text": [
                        "/home/u/anaconda/envs/recommenders/lib/python3.9/site-packages/pyspark/sql/context.py:125: FutureWarning: Deprecated in 3.0.0. Use SparkSession.builder.getOrCreate() instead.\n",
                        "  warnings.warn(\n"
                    ]
                }
            ],
            "source": [
                "spark_rate_eval = SparkRatingEvaluation(dfs_true, dfs_pred, **HEADER)"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "#### 2.1.3 Root Mean Square Error (RMSE)\n",
                "\n",
                "RMSE is for evaluating the accuracy of prediction on ratings. RMSE is the most widely used metric to evaluate a recommendation algorithm that predicts missing ratings. The benefit is that RMSE is easy to explain and calculate."
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 10,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "The RMSE is 7.254309064273455\n"
                    ]
                }
            ],
            "source": [
                "print(f\"The RMSE is {spark_rate_eval.rmse()}\")"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "#### 2.1.4 R Squared (R2)\n",
                "\n",
                "R2 is also called \"coefficient of determination\" in some context. It is a metric that evaluates how well a regression model performs, based on the proportion of total variations of the observed results. "
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 11,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "The R2 is -31.699029126213595\n"
                    ]
                }
            ],
            "source": [
                "print(f\"The R2 is {spark_rate_eval.rsquared()}\")"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "#### 2.1.5 Mean Absolute Error (MAE)\n",
                "\n",
                "MAE evaluates accuracy of prediction. It computes the metric value from ground truths and prediction in the same scale. Compared to RMSE, MAE is more explainable. "
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 12,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "The MAE is 6.375\n"
                    ]
                }
            ],
            "source": [
                "print(f\"The MAE is {spark_rate_eval.mae()}\")"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "#### 2.1.6 Explained Variance \n",
                "\n",
                "Explained variance is usually used to measure how well a model performs with regard to the impact from the variation of the dataset. "
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 13,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "The explained variance is -6.4466019417475735\n"
                    ]
                }
            ],
            "source": [
                "print(f\"The explained variance is {spark_rate_eval.exp_var()}\")"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "#### 2.1.7 Summary"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "|Metric|Range|Selection criteria|Limitation|Reference|\n",
                "|------|-------------------------------|---------|----------|---------|\n",
                "|RMSE|$> 0$|The smaller the better.|May be biased, and less explainable than MAE|[link](https://en.wikipedia.org/wiki/Root-mean-square_deviation)|\n",
                "|R2|$\\leq 1$|The closer to $1$ the better.|Depend on variable distributions.|[link](https://en.wikipedia.org/wiki/Coefficient_of_determination)|\n",
                "|MAE|$\\geq 0$|The smaller the better.|Dependent on variable scale.|[link](https://en.wikipedia.org/wiki/Mean_absolute_error)|\n",
                "|Explained variance|$\\leq 1$|The closer to $1$ the better.|Depend on variable distributions.|[link](https://en.wikipedia.org/wiki/Explained_variation)|"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "### 2.2 Ranking metrics"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "\"Beyond-accuray evaluation\" was proposed to evaluate how relevant recommendations are for users. In this case, a recommendation system is a treated as a ranking system. Given relency definition, recommendation system outputs a list of recommended items to each user, which is ordered by relevance. The evaluation part takes ground-truth data, the actual items that users interact with (e.g., liked, purchased, etc.), and the recommendation data, as inputs, to calculate ranking evaluation metrics. \n",
                "\n",
                "#### 2.2.1 Use cases\n",
                "\n",
                "Ranking metrics are often used when hit and/or ranking of the items are considered:\n",
                "* **Hit** - defined by relevancy, a hit usually means whether the recommended \"k\" items hit the \"relevant\" items by the user. For example, a user may have clicked, viewed, or purchased an item for many times, and a hit in the recommended items indicate that the recommender performs well. Metrics like \"precision\", \"recall\", etc. measure the performance of such hitting accuracy.\n",
                "* **Ranking** - ranking metrics give more explanations about, for the hitted items, whether they are ranked in a way that is preferred by the users whom the items will be recommended to. Metrics like \"mean average precision\", \"ndcg\", etc., evaluate whether the relevant items are ranked higher than the less-relevant or irrelevant items. \n",
                "\n",
                "#### 2.2.2 How-to with evaluation utilities\n",
                "\n",
                "A few notes about the interface of the Rating evaluator class:\n",
                "1. The columns of user, item, and rating (prediction) should be present in the ground-truth DataFrame (prediction DataFrame). The column of timestamp is optional, but it is required if certain relevant function is used. For example, timestamps will be used if the most recent items are defined as the relevant one.\n",
                "2. There should be no duplicates of (user, item) pairs in the ground-truth and the prediction DataFrames, othewise there may be unexpected behavior in calculating certain metrics.\n",
                "3. Default column names for user, item, rating, and prediction are \"UserId\", \"ItemId\", \"Rating\", and \"Prediciton\", respectively."
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "#### 2.2.3 Relevancy of recommendation\n",
                "\n",
                "Relevancy of recommendation can be measured in different ways:\n",
                "\n",
                "* **By ranking** - In this case, relevant items in the recommendations are defined as the top ranked items, i.e., top k items, which are taken from the list of the recommended items that is ordered by the predicted ratings (or other numerical scores that indicate preference of a user to an item). \n",
                "\n",
                "* **By timestamp** - Relevant items are defined as the most recently viewed k items, which are obtained from the recommended items ranked by timestamps.\n",
                "\n",
                "* **By rating** - Relevant items are defined as items with ratings (or other numerical scores that indicate preference of a user to an item) that are above a given threshold. \n",
                "\n",
                "Similarly, a ranking metric object can be initialized as below. The input data schema is\n",
                "\n",
                "* Ground-truth dataset.\n",
                "\n",
                "|Column|Data type|Description|\n",
                "|-------------|------------|-------------|\n",
                "|`COL_USER`|<int\\>|User ID|\n",
                "|`COL_ITEM`|<int\\>|Item ID|\n",
                "|`COL_RATING`|<float\\>|Rating or numerical value of user preference.|\n",
                "|`COL_TIMESTAMP`|<string\\>|Timestamps.|\n",
                "\n",
                "* Prediction dataset.\n",
                "\n",
                "|Column|Data type|Description|\n",
                "|-------------|------------|-------------|\n",
                "|`COL_USER`|<int\\>|User ID|\n",
                "|`COL_ITEM`|<int\\>|Item ID|\n",
                "|`COL_RATING`|<float\\>|Predicted rating or numerical value of user preference.|\n",
                "|`COL_TIMESTAM`|<string\\>|Timestamps.|\n",
                "\n",
                "In this case, in addition to the input datasets, there are also other arguments used for calculating the ranking metrics:\n",
                "\n",
                "|Argument|Data type|Description|\n",
                "|------------|------------|--------------|\n",
                "|`k`|<int\\>|Number of items recommended to user.|\n",
                "|`revelancy_method`|<string\\>|Methonds that extract relevant items from the recommendation list|"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "For example, the following code initializes a ranking metric object that calculates the metrics."
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 14,
            "metadata": {},
            "outputs": [],
            "source": [
                "spark_rank_eval = SparkRankingEvaluation(dfs_true, dfs_pred, k=3, relevancy_method=\"top_k\", **HEADER)"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "A few ranking metrics can then be calculated."
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "#### 2.2.4 Precision\n",
                "\n",
                "Precision@k is a metric that evaluates how many items in the recommendation list are relevant (hit) in the ground-truth data. For each user the precision score is normalized by `k` and then the overall precision scores are averaged by the total number of users. \n",
                "\n",
                "Note it is apparent that the precision@k metric grows with the number of `k`."
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 15,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "The precision at k is 0.3333333333333333\n"
                    ]
                }
            ],
            "source": [
                "print(f\"The precision at k is {spark_rank_eval.precision_at_k()}\")"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "#### 2.2.5 Recall\n",
                "\n",
                "Recall@k is a metric that evaluates how many relevant items in the ground-truth data are in the recommendation list. For each user the recall score is normalized by the total number of ground-truth items and then the overall recall scores are averaged by the total number of users. "
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 16,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "The recall at k is 0.2111111111111111\n"
                    ]
                }
            ],
            "source": [
                "print(f\"The recall at k is {spark_rank_eval.recall_at_k()}\")"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "#### 2.2.6 Normalized Discounted Cumulative Gain (NDCG)\n",
                "\n",
                "NDCG is a metric that evaluates how well the recommender performs in recommending ranked items to users. Therefore both hit of relevant items and correctness in ranking of these items matter to the NDCG evaluation. The total NDCG score is normalized by the total number of users."
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 17,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "The NDCG at k is 0.3333333333333333\n"
                    ]
                }
            ],
            "source": [
                "print(f\"The NDCG at k is {spark_rank_eval.ndcg_at_k()}\")"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "#### 2.2.7 Mean Average Precision (MAP)\n",
                "\n",
                "MAP is a metric that evaluates the average precision for each user in the datasets. It also penalizes ranking correctness of the recommended items. The overall MAP score is normalized by the total number of users."
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 18,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "The MAP at k is 0.15\n"
                    ]
                }
            ],
            "source": [
                "print(f\"The MAP at k is {spark_rank_eval.map_at_k()}\")"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "#### 2.2.8 ROC and AUC\n",
                "\n",
                "ROC, as well as AUC, is a well known metric that is used for evaluating binary classification problem. It is similar in the case of binary rating typed recommendation algorithm where the \"hit\" accuracy on the relevant items is used for measuring the recommender's performance. \n",
                "\n",
                "To demonstrate the evaluation method, the original data for testing is manipuldated in a way that the ratings in the testing data are arranged as binary scores, whilst the ones in the prediction are scaled in 0 to 1. "
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 19,
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "text/html": [
                            "<div>\n",
                            "<style scoped>\n",
                            "    .dataframe tbody tr th:only-of-type {\n",
                            "        vertical-align: middle;\n",
                            "    }\n",
                            "\n",
                            "    .dataframe tbody tr th {\n",
                            "        vertical-align: top;\n",
                            "    }\n",
                            "\n",
                            "    .dataframe thead th {\n",
                            "        text-align: right;\n",
                            "    }\n",
                            "</style>\n",
                            "<table border=\"1\" class=\"dataframe\">\n",
                            "  <thead>\n",
                            "    <tr style=\"text-align: right;\">\n",
                            "      <th></th>\n",
                            "      <th>UserId</th>\n",
                            "      <th>MovieId</th>\n",
                            "      <th>Rating</th>\n",
                            "    </tr>\n",
                            "  </thead>\n",
                            "  <tbody>\n",
                            "    <tr>\n",
                            "      <th>0</th>\n",
                            "      <td>1</td>\n",
                            "      <td>1</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>1</th>\n",
                            "      <td>1</td>\n",
                            "      <td>2</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>2</th>\n",
                            "      <td>1</td>\n",
                            "      <td>3</td>\n",
                            "      <td>0</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>3</th>\n",
                            "      <td>2</td>\n",
                            "      <td>1</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>4</th>\n",
                            "      <td>2</td>\n",
                            "      <td>4</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>5</th>\n",
                            "      <td>2</td>\n",
                            "      <td>5</td>\n",
                            "      <td>0</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>6</th>\n",
                            "      <td>2</td>\n",
                            "      <td>6</td>\n",
                            "      <td>0</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>7</th>\n",
                            "      <td>2</td>\n",
                            "      <td>7</td>\n",
                            "      <td>0</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>8</th>\n",
                            "      <td>3</td>\n",
                            "      <td>2</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>9</th>\n",
                            "      <td>3</td>\n",
                            "      <td>5</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>10</th>\n",
                            "      <td>3</td>\n",
                            "      <td>6</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>11</th>\n",
                            "      <td>3</td>\n",
                            "      <td>8</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>12</th>\n",
                            "      <td>3</td>\n",
                            "      <td>9</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>13</th>\n",
                            "      <td>3</td>\n",
                            "      <td>10</td>\n",
                            "      <td>0</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>14</th>\n",
                            "      <td>3</td>\n",
                            "      <td>11</td>\n",
                            "      <td>0</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>15</th>\n",
                            "      <td>3</td>\n",
                            "      <td>12</td>\n",
                            "      <td>0</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>16</th>\n",
                            "      <td>3</td>\n",
                            "      <td>13</td>\n",
                            "      <td>0</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>17</th>\n",
                            "      <td>3</td>\n",
                            "      <td>14</td>\n",
                            "      <td>0</td>\n",
                            "    </tr>\n",
                            "  </tbody>\n",
                            "</table>\n",
                            "</div>"
                        ],
                        "text/plain": [
                            "    UserId  MovieId  Rating\n",
                            "0        1        1       1\n",
                            "1        1        2       1\n",
                            "2        1        3       0\n",
                            "3        2        1       1\n",
                            "4        2        4       1\n",
                            "5        2        5       0\n",
                            "6        2        6       0\n",
                            "7        2        7       0\n",
                            "8        3        2       1\n",
                            "9        3        5       1\n",
                            "10       3        6       1\n",
                            "11       3        8       1\n",
                            "12       3        9       1\n",
                            "13       3       10       0\n",
                            "14       3       11       0\n",
                            "15       3       12       0\n",
                            "16       3       13       0\n",
                            "17       3       14       0"
                        ]
                    },
                    "execution_count": 19,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "# Convert the original rating to 0 and 1.\n",
                "df_true_bin = df_true.copy()\n",
                "df_true_bin[COL_RATING] = df_true_bin[COL_RATING].apply(lambda x: 1 if x > 3 else 0)\n",
                "\n",
                "df_true_bin"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 20,
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "text/html": [
                            "<div>\n",
                            "<style scoped>\n",
                            "    .dataframe tbody tr th:only-of-type {\n",
                            "        vertical-align: middle;\n",
                            "    }\n",
                            "\n",
                            "    .dataframe tbody tr th {\n",
                            "        vertical-align: top;\n",
                            "    }\n",
                            "\n",
                            "    .dataframe thead th {\n",
                            "        text-align: right;\n",
                            "    }\n",
                            "</style>\n",
                            "<table border=\"1\" class=\"dataframe\">\n",
                            "  <thead>\n",
                            "    <tr style=\"text-align: right;\">\n",
                            "      <th></th>\n",
                            "      <th>UserId</th>\n",
                            "      <th>MovieId</th>\n",
                            "      <th>Rating</th>\n",
                            "    </tr>\n",
                            "  </thead>\n",
                            "  <tbody>\n",
                            "    <tr>\n",
                            "      <th>0</th>\n",
                            "      <td>1</td>\n",
                            "      <td>3</td>\n",
                            "      <td>1.000000</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>1</th>\n",
                            "      <td>1</td>\n",
                            "      <td>10</td>\n",
                            "      <td>0.888889</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>2</th>\n",
                            "      <td>1</td>\n",
                            "      <td>12</td>\n",
                            "      <td>0.777778</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>3</th>\n",
                            "      <td>2</td>\n",
                            "      <td>10</td>\n",
                            "      <td>1.000000</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>4</th>\n",
                            "      <td>2</td>\n",
                            "      <td>3</td>\n",
                            "      <td>0.888889</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>5</th>\n",
                            "      <td>2</td>\n",
                            "      <td>5</td>\n",
                            "      <td>0.777778</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>6</th>\n",
                            "      <td>2</td>\n",
                            "      <td>11</td>\n",
                            "      <td>0.666667</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>7</th>\n",
                            "      <td>2</td>\n",
                            "      <td>13</td>\n",
                            "      <td>0.555556</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>8</th>\n",
                            "      <td>3</td>\n",
                            "      <td>4</td>\n",
                            "      <td>1.000000</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>9</th>\n",
                            "      <td>3</td>\n",
                            "      <td>10</td>\n",
                            "      <td>0.888889</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>10</th>\n",
                            "      <td>3</td>\n",
                            "      <td>7</td>\n",
                            "      <td>0.777778</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>11</th>\n",
                            "      <td>3</td>\n",
                            "      <td>13</td>\n",
                            "      <td>0.666667</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>12</th>\n",
                            "      <td>3</td>\n",
                            "      <td>1</td>\n",
                            "      <td>0.555556</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>13</th>\n",
                            "      <td>3</td>\n",
                            "      <td>3</td>\n",
                            "      <td>0.444444</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>14</th>\n",
                            "      <td>3</td>\n",
                            "      <td>5</td>\n",
                            "      <td>0.333333</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>15</th>\n",
                            "      <td>3</td>\n",
                            "      <td>2</td>\n",
                            "      <td>0.222222</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>16</th>\n",
                            "      <td>3</td>\n",
                            "      <td>11</td>\n",
                            "      <td>0.111111</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>17</th>\n",
                            "      <td>3</td>\n",
                            "      <td>14</td>\n",
                            "      <td>0.000000</td>\n",
                            "    </tr>\n",
                            "  </tbody>\n",
                            "</table>\n",
                            "</div>"
                        ],
                        "text/plain": [
                            "    UserId  MovieId    Rating\n",
                            "0        1        3  1.000000\n",
                            "1        1       10  0.888889\n",
                            "2        1       12  0.777778\n",
                            "3        2       10  1.000000\n",
                            "4        2        3  0.888889\n",
                            "5        2        5  0.777778\n",
                            "6        2       11  0.666667\n",
                            "7        2       13  0.555556\n",
                            "8        3        4  1.000000\n",
                            "9        3       10  0.888889\n",
                            "10       3        7  0.777778\n",
                            "11       3       13  0.666667\n",
                            "12       3        1  0.555556\n",
                            "13       3        3  0.444444\n",
                            "14       3        5  0.333333\n",
                            "15       3        2  0.222222\n",
                            "16       3       11  0.111111\n",
                            "17       3       14  0.000000"
                        ]
                    },
                    "execution_count": 20,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "# Convert the predicted ratings into a [0, 1] scale.\n",
                "df_pred_bin = df_pred.copy()\n",
                "df_pred_bin[COL_PREDICTION] = minmax_scale(df_pred_bin[COL_PREDICTION].astype(float))\n",
                "\n",
                "df_pred_bin"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 24,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "The auc score is 0.33333333333333337\n"
                    ]
                }
            ],
            "source": [
                "# Calculate the AUC metric\n",
                "auc_score = auc(\n",
                "    df_true_bin,\n",
                "    df_pred_bin,\n",
                "    col_user = COL_USER,\n",
                "    col_item = COL_ITEM,\n",
                "    col_rating = COL_RATING,\n",
                "    col_prediction = COL_RATING\n",
                ")\n",
                "\n",
                "print(f\"The auc score is {auc_score}\")"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "It is worth mentioning that in some literature there are variants of the original AUC metric, that considers the effect of **the number of the recommended items (k)**, **grouping effect of users (compute AUC for each user group, and take the average across different groups)**. These variants are applicable to various different scenarios, and choosing an appropriate one depends on the context of the use case itself."
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "#### 2.3.2 Logistic loss"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "Logistic loss (sometimes it is called simply logloss, or cross-entropy loss) is another useful metric to evaluate the hit accuracy. It is defined as the negative log-likelihood of the true labels given the predictions of a classifier."
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 25,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "The logloss score is 5.2574953720277815\n"
                    ]
                }
            ],
            "source": [
                "# Calculate the logloss metric\n",
                "logloss_score = logloss(\n",
                "    df_true_bin,\n",
                "    df_pred_bin,\n",
                "    col_user = COL_USER,\n",
                "    col_item = COL_ITEM,\n",
                "    col_rating = COL_RATING,\n",
                "    col_prediction = COL_RATING\n",
                ")\n",
                "\n",
                "print(f\"The logloss score is {logloss_score}\")"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "It is worth noting that logloss may be sensitive to the class balance of datasets, as it penalizes heavily classifiers that are confident about incorrect classifications. To demonstrate, the ground truth data set for testing is manipulated purposely to unbalance the binary labels. For example, the following binarizes the original rating data by using a lower threshold, i.e., 2, to create more positive feedback from the user."
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 26,
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "text/html": [
                            "<div>\n",
                            "<style scoped>\n",
                            "    .dataframe tbody tr th:only-of-type {\n",
                            "        vertical-align: middle;\n",
                            "    }\n",
                            "\n",
                            "    .dataframe tbody tr th {\n",
                            "        vertical-align: top;\n",
                            "    }\n",
                            "\n",
                            "    .dataframe thead th {\n",
                            "        text-align: right;\n",
                            "    }\n",
                            "</style>\n",
                            "<table border=\"1\" class=\"dataframe\">\n",
                            "  <thead>\n",
                            "    <tr style=\"text-align: right;\">\n",
                            "      <th></th>\n",
                            "      <th>UserId</th>\n",
                            "      <th>MovieId</th>\n",
                            "      <th>Rating</th>\n",
                            "    </tr>\n",
                            "  </thead>\n",
                            "  <tbody>\n",
                            "    <tr>\n",
                            "      <th>0</th>\n",
                            "      <td>1</td>\n",
                            "      <td>1</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>1</th>\n",
                            "      <td>1</td>\n",
                            "      <td>2</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>2</th>\n",
                            "      <td>1</td>\n",
                            "      <td>3</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>3</th>\n",
                            "      <td>2</td>\n",
                            "      <td>1</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>4</th>\n",
                            "      <td>2</td>\n",
                            "      <td>4</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>5</th>\n",
                            "      <td>2</td>\n",
                            "      <td>5</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>6</th>\n",
                            "      <td>2</td>\n",
                            "      <td>6</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>7</th>\n",
                            "      <td>2</td>\n",
                            "      <td>7</td>\n",
                            "      <td>0</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>8</th>\n",
                            "      <td>3</td>\n",
                            "      <td>2</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>9</th>\n",
                            "      <td>3</td>\n",
                            "      <td>5</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>10</th>\n",
                            "      <td>3</td>\n",
                            "      <td>6</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>11</th>\n",
                            "      <td>3</td>\n",
                            "      <td>8</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>12</th>\n",
                            "      <td>3</td>\n",
                            "      <td>9</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>13</th>\n",
                            "      <td>3</td>\n",
                            "      <td>10</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>14</th>\n",
                            "      <td>3</td>\n",
                            "      <td>11</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>15</th>\n",
                            "      <td>3</td>\n",
                            "      <td>12</td>\n",
                            "      <td>1</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>16</th>\n",
                            "      <td>3</td>\n",
                            "      <td>13</td>\n",
                            "      <td>0</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>17</th>\n",
                            "      <td>3</td>\n",
                            "      <td>14</td>\n",
                            "      <td>0</td>\n",
                            "    </tr>\n",
                            "  </tbody>\n",
                            "</table>\n",
                            "</div>"
                        ],
                        "text/plain": [
                            "    UserId  MovieId  Rating\n",
                            "0        1        1       1\n",
                            "1        1        2       1\n",
                            "2        1        3       1\n",
                            "3        2        1       1\n",
                            "4        2        4       1\n",
                            "5        2        5       1\n",
                            "6        2        6       1\n",
                            "7        2        7       0\n",
                            "8        3        2       1\n",
                            "9        3        5       1\n",
                            "10       3        6       1\n",
                            "11       3        8       1\n",
                            "12       3        9       1\n",
                            "13       3       10       1\n",
                            "14       3       11       1\n",
                            "15       3       12       1\n",
                            "16       3       13       0\n",
                            "17       3       14       0"
                        ]
                    },
                    "execution_count": 26,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "df_true_bin_pos = df_true.copy()\n",
                "df_true_bin_pos[COL_RATING] = df_true_bin_pos[COL_RATING].apply(lambda x: 1 if x > 2 else 0)\n",
                "\n",
                "df_true_bin_pos"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "By using threshold of 2, the labels in the ground truth data is not balanced, and the ratio of 1 over 0 is "
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 27,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "The ratio between label 1 and label 0 is 5.0\n"
                    ]
                }
            ],
            "source": [
                "one_zero_ratio = df_true_bin_pos[COL_PREDICTION].sum() / (df_true_bin_pos.shape[0] - df_true_bin_pos[COL_PREDICTION].sum())\n",
                "\n",
                "print(f\"The ratio between label 1 and label 0 is {one_zero_ratio}\")"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "Another prediction data is also created, where the probabilities for label 1 and label 0 are fixed. Without loss of generity, the probability of predicting 1 is 0.6. The data set is purposely created to make the precision to be 100% given an presumption of cut-off equal to 0.5."
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 28,
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "text/html": [
                            "<div>\n",
                            "<style scoped>\n",
                            "    .dataframe tbody tr th:only-of-type {\n",
                            "        vertical-align: middle;\n",
                            "    }\n",
                            "\n",
                            "    .dataframe tbody tr th {\n",
                            "        vertical-align: top;\n",
                            "    }\n",
                            "\n",
                            "    .dataframe thead th {\n",
                            "        text-align: right;\n",
                            "    }\n",
                            "</style>\n",
                            "<table border=\"1\" class=\"dataframe\">\n",
                            "  <thead>\n",
                            "    <tr style=\"text-align: right;\">\n",
                            "      <th></th>\n",
                            "      <th>UserId</th>\n",
                            "      <th>MovieId</th>\n",
                            "      <th>Rating</th>\n",
                            "    </tr>\n",
                            "  </thead>\n",
                            "  <tbody>\n",
                            "    <tr>\n",
                            "      <th>0</th>\n",
                            "      <td>1</td>\n",
                            "      <td>1</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>1</th>\n",
                            "      <td>1</td>\n",
                            "      <td>2</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>2</th>\n",
                            "      <td>1</td>\n",
                            "      <td>3</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>3</th>\n",
                            "      <td>2</td>\n",
                            "      <td>1</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>4</th>\n",
                            "      <td>2</td>\n",
                            "      <td>4</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>5</th>\n",
                            "      <td>2</td>\n",
                            "      <td>5</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>6</th>\n",
                            "      <td>2</td>\n",
                            "      <td>6</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>7</th>\n",
                            "      <td>2</td>\n",
                            "      <td>7</td>\n",
                            "      <td>0.4</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>8</th>\n",
                            "      <td>3</td>\n",
                            "      <td>2</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>9</th>\n",
                            "      <td>3</td>\n",
                            "      <td>5</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>10</th>\n",
                            "      <td>3</td>\n",
                            "      <td>6</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>11</th>\n",
                            "      <td>3</td>\n",
                            "      <td>8</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>12</th>\n",
                            "      <td>3</td>\n",
                            "      <td>9</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>13</th>\n",
                            "      <td>3</td>\n",
                            "      <td>10</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>14</th>\n",
                            "      <td>3</td>\n",
                            "      <td>11</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>15</th>\n",
                            "      <td>3</td>\n",
                            "      <td>12</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>16</th>\n",
                            "      <td>3</td>\n",
                            "      <td>13</td>\n",
                            "      <td>0.4</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>17</th>\n",
                            "      <td>3</td>\n",
                            "      <td>14</td>\n",
                            "      <td>0.4</td>\n",
                            "    </tr>\n",
                            "  </tbody>\n",
                            "</table>\n",
                            "</div>"
                        ],
                        "text/plain": [
                            "    UserId  MovieId  Rating\n",
                            "0        1        1     0.6\n",
                            "1        1        2     0.6\n",
                            "2        1        3     0.6\n",
                            "3        2        1     0.6\n",
                            "4        2        4     0.6\n",
                            "5        2        5     0.6\n",
                            "6        2        6     0.6\n",
                            "7        2        7     0.4\n",
                            "8        3        2     0.6\n",
                            "9        3        5     0.6\n",
                            "10       3        6     0.6\n",
                            "11       3        8     0.6\n",
                            "12       3        9     0.6\n",
                            "13       3       10     0.6\n",
                            "14       3       11     0.6\n",
                            "15       3       12     0.6\n",
                            "16       3       13     0.4\n",
                            "17       3       14     0.4"
                        ]
                    },
                    "execution_count": 28,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "prob_true = 0.6\n",
                "\n",
                "df_pred_bin_pos = df_true_bin_pos.copy()\n",
                "df_pred_bin_pos[COL_PREDICTION] = df_pred_bin_pos[COL_PREDICTION].apply(lambda x: prob_true if x==1 else 1-prob_true)\n",
                "\n",
                "df_pred_bin_pos"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "Then the logloss is calculated as follows. "
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 29,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "The logloss score is 5.2574953720277815\n"
                    ]
                }
            ],
            "source": [
                "# Calculate the logloss metric\n",
                "logloss_score_pos = logloss(\n",
                "    df_true_bin_pos,\n",
                "    df_pred_bin_pos,\n",
                "    col_user = COL_USER,\n",
                "    col_item = COL_ITEM,\n",
                "    col_rating = COL_RATING,\n",
                "    col_prediction = COL_RATING\n",
                ")\n",
                "\n",
                "print(f\"The logloss score is {logloss_score}\")"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "For comparison, a similar process is used with a threshold value of 3 to create a more balanced dataset. Another prediction dataset is also created by using the balanced dataset. Again, the probabilities of predicting label 1 and label 0 are fixed as 0.6 and 0.4, respectively. **NOTE**, same as above, in this case, the prediction also gives us a 100% precision. The only difference is the proportion of binary labels."
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 32,
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "text/html": [
                            "<div>\n",
                            "<style scoped>\n",
                            "    .dataframe tbody tr th:only-of-type {\n",
                            "        vertical-align: middle;\n",
                            "    }\n",
                            "\n",
                            "    .dataframe tbody tr th {\n",
                            "        vertical-align: top;\n",
                            "    }\n",
                            "\n",
                            "    .dataframe thead th {\n",
                            "        text-align: right;\n",
                            "    }\n",
                            "</style>\n",
                            "<table border=\"1\" class=\"dataframe\">\n",
                            "  <thead>\n",
                            "    <tr style=\"text-align: right;\">\n",
                            "      <th></th>\n",
                            "      <th>UserId</th>\n",
                            "      <th>MovieId</th>\n",
                            "      <th>Rating</th>\n",
                            "    </tr>\n",
                            "  </thead>\n",
                            "  <tbody>\n",
                            "    <tr>\n",
                            "      <th>0</th>\n",
                            "      <td>1</td>\n",
                            "      <td>1</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>1</th>\n",
                            "      <td>1</td>\n",
                            "      <td>2</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>2</th>\n",
                            "      <td>1</td>\n",
                            "      <td>3</td>\n",
                            "      <td>0.4</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>3</th>\n",
                            "      <td>2</td>\n",
                            "      <td>1</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>4</th>\n",
                            "      <td>2</td>\n",
                            "      <td>4</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>5</th>\n",
                            "      <td>2</td>\n",
                            "      <td>5</td>\n",
                            "      <td>0.4</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>6</th>\n",
                            "      <td>2</td>\n",
                            "      <td>6</td>\n",
                            "      <td>0.4</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>7</th>\n",
                            "      <td>2</td>\n",
                            "      <td>7</td>\n",
                            "      <td>0.4</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>8</th>\n",
                            "      <td>3</td>\n",
                            "      <td>2</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>9</th>\n",
                            "      <td>3</td>\n",
                            "      <td>5</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>10</th>\n",
                            "      <td>3</td>\n",
                            "      <td>6</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>11</th>\n",
                            "      <td>3</td>\n",
                            "      <td>8</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>12</th>\n",
                            "      <td>3</td>\n",
                            "      <td>9</td>\n",
                            "      <td>0.6</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>13</th>\n",
                            "      <td>3</td>\n",
                            "      <td>10</td>\n",
                            "      <td>0.4</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>14</th>\n",
                            "      <td>3</td>\n",
                            "      <td>11</td>\n",
                            "      <td>0.4</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>15</th>\n",
                            "      <td>3</td>\n",
                            "      <td>12</td>\n",
                            "      <td>0.4</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>16</th>\n",
                            "      <td>3</td>\n",
                            "      <td>13</td>\n",
                            "      <td>0.4</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>17</th>\n",
                            "      <td>3</td>\n",
                            "      <td>14</td>\n",
                            "      <td>0.4</td>\n",
                            "    </tr>\n",
                            "  </tbody>\n",
                            "</table>\n",
                            "</div>"
                        ],
                        "text/plain": [
                            "    UserId  MovieId  Rating\n",
                            "0        1        1     0.6\n",
                            "1        1        2     0.6\n",
                            "2        1        3     0.4\n",
                            "3        2        1     0.6\n",
                            "4        2        4     0.6\n",
                            "5        2        5     0.4\n",
                            "6        2        6     0.4\n",
                            "7        2        7     0.4\n",
                            "8        3        2     0.6\n",
                            "9        3        5     0.6\n",
                            "10       3        6     0.6\n",
                            "11       3        8     0.6\n",
                            "12       3        9     0.6\n",
                            "13       3       10     0.4\n",
                            "14       3       11     0.4\n",
                            "15       3       12     0.4\n",
                            "16       3       13     0.4\n",
                            "17       3       14     0.4"
                        ]
                    },
                    "execution_count": 32,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "prob_true = 0.6\n",
                "\n",
                "df_pred_bin_balanced = df_true_bin.copy()\n",
                "df_pred_bin_balanced[COL_PREDICTION] = df_pred_bin_balanced[COL_PREDICTION].apply(lambda x: prob_true if x==1 else 1-prob_true)\n",
                "\n",
                "df_pred_bin_balanced"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "The ratio of label 1 and label 0 is"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 33,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "The ratio between label 1 and label 0 is 1.0\n"
                    ]
                }
            ],
            "source": [
                "one_zero_ratio = df_true_bin[COL_PREDICTION].sum() / (df_true_bin.shape[0] - df_true_bin[COL_PREDICTION].sum())\n",
                "\n",
                "print(f\"The ratio between label 1 and label 0 is {one_zero_ratio}\")"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "It is perfectly balanced."
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "Applying the logloss function to calculate the metric gives us a more promising result, as shown below."
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 34,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "The logloss score is 0.5108256237659907\n"
                    ]
                }
            ],
            "source": [
                "# Calculate the logloss metric\n",
                "logloss_score = logloss(\n",
                "    df_true_bin,\n",
                "    df_pred_bin_balanced,\n",
                "    col_user = COL_USER,\n",
                "    col_item = COL_ITEM,\n",
                "    col_rating = COL_RATING,\n",
                "    col_prediction = COL_RATING\n",
                ")\n",
                "\n",
                "print(f\"The logloss score is {logloss_score}\")"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "It can be seen that the score is more close to 0, and, by definition, it means that the predictions are generating better results than the one before where binary labels are more biased."
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "#### 2.3 Summary"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "|Metric|Range|Selection criteria|Limitation|Reference|\n",
                "|------|-------------------------------|---------|----------|---------|\n",
                "|Precision|$\\geq 0$ and $\\leq 1$|The closer to $1$ the better.|Only for hits in recommendations.|[link](https://spark.apache.org/docs/2.3.0/mllib-evaluation-metrics.html#ranking-systems)|\n",
                "|Recall|$\\geq 0$ and $\\leq 1$|The closer to $1$ the better.|Only for hits in the ground truth.|[link](https://en.wikipedia.org/wiki/Precision_and_recall)|\n",
                "|NDCG|$\\geq 0$ and $\\leq 1$|The closer to $1$ the better.|Does not penalize for bad/missing items, and does not perform for several equally good items.|[link](https://spark.apache.org/docs/2.3.0/mllib-evaluation-metrics.html#ranking-systems)|\n",
                "|MAP|$\\geq 0$ and $\\leq 1$|The closer to $1$ the better.|Depend on variable distributions.|[link](https://spark.apache.org/docs/2.3.0/mllib-evaluation-metrics.html#ranking-systems)|\n",
                "|AUC|$\\geq 0$ and $\\leq 1$|The closer to $1$ the better. 0.5 indicates an uninformative classifier|Depend on the number of recommended items (k).|[link](https://en.wikipedia.org/wiki/Receiver_operating_characteristic#Area_under_the_curve)|\n",
                "|Logloss|$0$ to $\\infty$|The closer to $0$ the better.|Logloss can be sensitive to imbalanced datasets.|[link](https://en.wikipedia.org/wiki/Cross_entropy#Relation_to_log-likelihood)|"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 31,
            "metadata": {},
            "outputs": [],
            "source": [
                "# cleanup spark instance\n",
                "spark.stop()"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## References\n",
                "\n",
                "1. Guy Shani and Asela Gunawardana, \"Evaluating Recommendation Systems\", Recommender Systems Handbook, Springer, 2015.\n",
                "2. PySpark MLlib evaluation metrics, url: https://spark.apache.org/docs/2.3.0/mllib-evaluation-metrics.html.\n",
                "3. Dimitris Paraschakis et al, \"Comparative Evaluation of Top-N Recommenders in e-Commerce: An Industrial Perspective\", IEEE ICMLA, 2015, Miami, FL, USA.\n",
                "4. Yehuda Koren and Robert Bell, \"Advances in Collaborative Filtering\", Recommender Systems Handbook, Springer, 2015.\n",
                "5. Chris Bishop, \"Pattern Recognition and Machine Learning\", Springer, 2006."
            ]
        }
    ],
    "metadata": {
        "kernelspec": {
            "display_name": "Python (recommenders)",
            "language": "python",
            "name": "recommenders"
        },
        "language_info": {
            "codemirror_mode": {
                "name": "ipython",
                "version": 3
            },
            "file_extension": ".py",
            "mimetype": "text/x-python",
            "name": "python",
            "nbconvert_exporter": "python",
            "pygments_lexer": "ipython3",
            "version": "3.9.16"
        }
    },
    "nbformat": 4,
    "nbformat_minor": 2
}